
<?php
include("../app/connection.php");
class ExcelExport
{
	private $objPHPExcel;
	private $objWorksheet;
	private $objWriter;
	public function __construct()
	{
		require_once("../libraries/PHPExcel.php");
		$this->objPHPExcel = new PHPExcel();
		$this->objWorksheet=$this->objPHPExcel->getSheet(0);
	}
	public function writeByQuery($query)
	{
		require_once("exportForm.php");
		$a=new getForm();
		$result=mysql_query($query);
		$title=array();
		$haveSex = false;
		$sexIndex =0;
		$num = mysql_num_fields($result);
		for($i=0;$i<$num;$i++)
		{
			$tit = mysql_fetch_field($result,$i);
			$title[count($title)] = $a->convert($tit->name);
			if($tit->name =="GIOITINH")
			{
				$haveSex=true;
				$sexIndex = $i;
			}
		}
		$sheetContent=array();
		while($row = mysql_fetch_array($result))
		{
			$rowContent=array();
			for($j=0;$j<mysql_num_fields($result);$j++)
			{
				if($haveSex==true&&$j==$sexIndex)
				{
					if($row[$j]==0)
					{
						$rowContent[count($rowContent)] ="Nam";
					}
					else
					{
						$rowContent[count($rowContent)] ="Nữ";
					}
				}
				else $rowContent[count($rowContent)] = $row[$j];
			}
			$sheetContent[count($sheetContent)] = $rowContent;
		}
		$this->objWorksheet->fromArray($title,null,"A1",false);
		for($i=2;$i<count($sheetContent)+2;$i++)
		{
			$this->objWorksheet->fromArray($sheetContent[$i-2],null,"A".$i,false);
		}
		$highestRow = $this->objWorksheet->getHighestRow();
		$highestColumn = $this->objWorksheet->getHighestColumn();
		$styleArray = array("borders"=>array("allborders"=>array("style"=>PHPExcel_Style_Border::BORDER_THIN)));
		$this->objWorksheet->getStyle("A1:".$highestColumn.$highestRow)->applyFromArray($styleArray);
	}
	public function write($title,$table,$colWillExport)
	{
		$this->objWorksheet->fromArray($title,null,"A1",false);
		$i=2;
		$result = mysql_query("select * from ".$table);
		while($row = mysql_fetch_array($result))
		{
			echo "Exporting Rows ".($i-1)."...<br>";
			$value = array();
			foreach($colWillExport as $index)
			{
				if($index==2)
				{
					if($table=="SINHVIEN"||$table=="GIAOVIEN"||$table=="QUANTRIVIEN")
					{
						if($row[$index]==1)$value[count($value)] = "Nữ";
						else $value[count($value)] = "Nam";
					}
					else
					{
						$value[count($value)] = $row[$index];
					}
				}
				else
				{
					$value[count($value)] = $row[$index];
				}
			}
			$this->objWorksheet->fromArray($value,null,"A".$i,false);
			$i++;
		}
		//while($row = mysql_fetch_assoc($result))
//		{
//			$value = array();
//			foreach($row as $str)
//			{
//				$value[count($value)] = $str;
//			}
//			$this->objWorksheet->fromArray($value,null,"A".$i,false);
//			$i++;
//		}
		$highestRow = $this->objWorksheet->getHighestRow();
		$highestColumn = $this->objWorksheet->getHighestColumn();
		$styleArray = array("borders"=>array("allborders"=>array("style"=>PHPExcel_Style_Border::BORDER_THIN)));
		$this->objWorksheet->getStyle("A1:".$highestColumn.$highestRow)->applyFromArray($styleArray);
	}
	public function export($path)
	{
		$this->objWriter = new PHPExcel_Writer_Excel2007($this->objPHPExcel);
		$this->objWriter->save($path);
	}
}
class PDFExport
{
	private $objWriter;
	private $content;
	public function __construct()
	{
		require_once("../libraries/PDF/MPDF56/mpdf.php");
		$this->objWriter = new mPDF();
	}
	public function writeByHTML($str)
	{
		$content="";
		$content = $content."<div style='width:90%'>";
		$content = $content."<div style='width:30%;float:left;text-align:center'>";
		$content = $content."<img src='../image/source/uit.jpg' width='100' height='100' align='center'/>";
		$content = $content."</div>";
		$content = $content."<div style='width:60%;float:right;vertical-align:middle'><h3><br><br>Trường Đại Học Công Nghệ Thông Tin</h3>";
		$content = $content."</div>";
		$content = $content."</div><br><br>";
		$str=$content.$str;
		$this->content = $str;
	}
	public function writeByQuery($query)
	{
		require_once("exportForm.php");
		$a=new getForm();
		$result=mysql_query($query);
		$haveSex = false;
		$sexIndex =0;
		$content="";
		$content = $content."<div style='width:90%'>";
		$content = $content."<div style='width:30%;float:left;text-align:center'>";
		$content = $content."<img src='../image/source/uit.jpg' width='100' height='100' align='center'/>";
		$content = $content."</div>";
		$content = $content."<div style='width:60%;float:right;vertical-align:middle'><h3><br><br>Trường Đại Học Công Nghệ Thông Tin</h3>";
		$content = $content."</div>";
		$content = $content."</div><br><br>";
		$content = $content."<table style='boder-spacing:1px solid black;border-top:1px solid black;border-left:1px solid black; margin:auto;text-align:center'>";
		$content = $content."<tr>";
		$num = mysql_num_fields($result);
		for($i=0;$i<$num;$i++)
		{
			$tit = mysql_fetch_field($result,$i);
			$content = $content."<td style='border-right:1px solid black;border-bottom:1px solid black;background:#0CF'>";
			$content = $content.$a->convert($tit->name);
			$content = $content."</td>"; 
			if($tit->name =="GIOITINH")
			{
				$haveSex=true;
				$sexIndex = $i;
			}
		}
		$content = $content."</tr>";
		while($row = mysql_fetch_array($result))
		{
			$content = $content."<tr>";
			for($j=0;$j<mysql_num_fields($result);$j++)
			{
				if($haveSex==true&&$j==$sexIndex)
				{
					if($row[$j]==0)
					{
						$content = $content."<td style='border-right:1px solid black;border-bottom:1px solid black'>";
						$content = $content."Nam";
						$content = $content."</td>"; 
					}
					else
					{
						$content = $content."<td style='border-right:1px solid black;border-bottom:1px solid black'>";
						$content = $content."Nữ";
						$content = $content."</td>";
					}
				}
				else
				{
				 	$content = $content."<td style='border-right:1px solid black;border-bottom:1px solid black'>";
					$content = $content.$row[$j];
					$content = $content."</td>";
				}
			}
			$content = $content."</tr>";
		}
		$content = $content."</table>";
		$this->content = $content;
	}
	public function write($title,$table)
	{
		$this->content="<center>
		<table border='1'>
		<tr>";
		foreach($title as $tit)
		{
			$this->content = $this->content."<td>".$tit."</td>";
		}
		$this->content = $this->content. "</tr>";
		$result = mysql_query("select * from ".$table);
		while($row = mysql_fetch_assoc($result))
		{
			$this->content = $this->content. "<tr>";
			foreach($row as $str)
			{
				$this->content = $this->content. "<td>";
				$this->content = $this->content. $str;
				$this->content = $this->content. "</td>";
			}
			$this->content = $this->content. "</tr>";
		}
		$this->content = $this->content. "</table>";
		$this->content = $this->content. "</center>";	
	}
	public function export($path)
	{
		//$this->objWriter->useAdobeCJK=true;
		$this->objWriter->SetAutoFont(AUTOFONT_ALL);
		$this->objWriter->WriteHTML($this->content);
		$this->objWriter->Output($path);
	}
}
class outputAFile
{
	public function __construct()
	{
	}
	public function output($path,$extension)
	{
		$fileSize=filesize($path);
		if(file_exists($path))
		{
			header("Content-type: application/octet-stream");
			header("content-disposition: attachment;filename=export.".$extension);
			header("content-transfer-encoding: binary");
			header("expires: 0");
			header("cache-control: must-revalidate");
			header("pragma: public");
			header("Content-length: ".$fileSize);
			readfile($path);
			exit();
		}
		else
		{
		}
		
	}
}
?>